package com.czt.mail.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author czt
 * @version 1.0
 * {@desc}
 * @date 2024/8/2 15:43
 */
@Component
public class HashRedis {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    public void testHashRedis() {
        HashOperations<Object, Object, Object> hashOperations = redisTemplate.opsForHash();
        //增加单个
        hashOperations.put("userId:1", "msgId:2", "msg2");
        Map<String, Object> map = new HashMap<>();
        map.put("msgId:3", "msg3");
        map.put("msgId:4", "msg4");
        //增加多个
        hashOperations.putAll("userId:1", map);

        //获取key中hashKey的value
        Object hashValue = hashOperations.get("userId:1", "msgId:3");
        //设置过期时间
        redisTemplate.expire("userId:1", 1, TimeUnit.DAYS);

        //获取key中所有的key和value
        Map<Object, Object> entries = hashOperations.entries("userId:1");
        for (Map.Entry<Object, Object> objectObjectEntry : entries.entrySet()) {

        }


    }

}
